<!---
	Copyright 2010 Edward Trudeau, Jeremy Battle
    This software is freely available under the MIT License: http://www.opensource.org/licenses/mit-license.php
    http://ayc.googlecode.com
    
--->

<cfcomponent displayname="ValidationDAO" hint="table ID column = ValidationId">

	<cffunction name="init" access="public" output="false" returntype="ValidationDAO">
		<cfargument name="dsn" type="string" required="true">
		<cfset variables.dsn = arguments.dsn>
		<cfreturn this>
	</cffunction>
	
	<cffunction name="create" access="public" output="false" returntype="struct">
		<cfargument name="Validation" type="Validation" required="true" />
		<cfset var result = StructNew() />
        <cfset var returnid = "" />
		<cfset var qCreate = "" />
		<cftry>
			<cfquery name="qCreate" datasource="#variables.dsn#" result="returnid">
				INSERT INTO aycValidations
					(
					FieldId,
					ValidationRule,
					ValidationVal,
					ValidationMsg
					)
				VALUES
					(
					<cfqueryparam value="#arguments.Validation.getFieldId()#" CFSQLType="cf_sql_integer" />,
					<cfqueryparam value="#arguments.Validation.getValidationRule()#" CFSQLType="cf_sql_varchar" />,
					<cfqueryparam value="#arguments.Validation.getValidationVal()#" CFSQLType="cf_sql_varchar" />,
					<cfqueryparam value="#arguments.Validation.getValidationMsg()#" CFSQLType="cf_sql_varchar" null="#not len(arguments.Validation.getValidationMsg())#" />
					)
			</cfquery>
			<cfcatch type="database">
            	<cfset result.success = false />
                <cfset result.catch = cfcatch />
                <cfset result.query = '
                            INSERT INTO aycValidations
                                (
                                FieldId,
                                ValidateAt,
                                Validation,
                                ValidationMsg
                                )
                            VALUES
                                (
                                <cfqueryparam value="#arguments.Validation.getFieldId()#" CFSQLType="cf_sql_integer" />,
                                <cfqueryparam value="#arguments.Validation.getValidationRule()#" CFSQLType="cf_sql_varchar" />,
								<cfqueryparam value="#arguments.Validation.getValidationVal()#" CFSQLType="cf_sql_varchar" />,
								<cfqueryparam value="#arguments.Validation.getValidationMsg()#" CFSQLType="cf_sql_varchar" null="#not len(arguments.Validation.getValidationMsg())#" />
								)' />
                <cfset result.Validation = "No identity value returned" />
				<cfreturn result />
			</cfcatch>
		</cftry>
        <cfset result.success = true />
        <cfset result.catch = "No errors found" />
        <cfset result.query = '
                    INSERT INTO aycValidations
                        (
                        FieldId,
                        ValidateAt,
                        Validation,
                        ValidationMsg
                        )
                    VALUES
                        (
                        <cfqueryparam value="#arguments.Validation.getFieldId()#" CFSQLType="cf_sql_integer" />,
                        <cfqueryparam value="#arguments.Validation.getValidationRule()#" CFSQLType="cf_sql_varchar" />,
						<cfqueryparam value="#arguments.Validation.getValidationVal()#" CFSQLType="cf_sql_varchar" />,
						<cfqueryparam value="#arguments.Validation.getValidationMsg()#" CFSQLType="cf_sql_varchar" null="#not len(arguments.Validation.getValidationMsg())#" />
						)' />
        
        <cfset result.ValidationId = returnid.IDENTITYCOL />
        
		<cfreturn result />
	</cffunction>

	<cffunction name="read" access="public" output="false" returntype="void">
		<cfargument name="Validation" type="Validation" required="true" />

		<cfset var qRead = "" />
		<cfset var strReturn = structNew() />
		<cftry>
			<cfquery name="qRead" datasource="#variables.dsn#">
				SELECT
					ValidationId,
					FieldId,
					ValidationRule,
					ValidationVal,
					ValidationMsg
				FROM	aycValidations
				WHERE	ValidationId = <cfqueryparam value="#arguments.Validation.getValidationId()#" CFSQLType="cf_sql_integer" />
			</cfquery>
			<cfcatch type="database">
				<!--- leave the bean as is and set an empty query for the conditional logic below --->
				<cfset qRead = queryNew("id") />
			</cfcatch>
		</cftry>
		<cfif qRead.recordCount>
			<cfset strReturn = queryRowToStruct(qRead)>
			<cfset arguments.Validation.init(argumentCollection=strReturn)>
		</cfif>
	</cffunction>

	<cffunction name="update" access="public" output="false" returntype="struct">
		<cfargument name="Validation" type="Validation" required="true" />
		<cfset var result = StructNew() />
		<cfset var qUpdate = "" />
		<cftry>
			<cfquery name="qUpdate" datasource="#variables.dsn#">
				UPDATE	aycValidations
				SET
					FieldId = <cfqueryparam value="#arguments.Validation.getFieldId()#" CFSQLType="cf_sql_integer" />,
					ValidationRule = <cfqueryparam value="#arguments.Validation.getValidateRule()#" CFSQLType="cf_sql_varchar" />,
					ValidationVal = <cfqueryparam value="#arguments.Validation.getValidationVal()#" CFSQLType="cf_sql_varchar" />,
					ValidationMsg = <cfqueryparam value="#arguments.Validation.getValidationMsg()#" CFSQLType="cf_sql_varchar" null="#not len(arguments.Validation.getValidationMsg())#" />
				WHERE	ValidationId = <cfqueryparam value="#arguments.Validation.getValidationId()#" CFSQLType="cf_sql_integer" />
			</cfquery>
			<cfcatch type="database">
            	<cfset result.success = false />
                <cfset result.catch = cfcatch />
            	<cfset result.query = '
               UPDATE	aycValidations
				SET
					FieldId = <cfqueryparam value="#arguments.Validation.getFieldId()#" CFSQLType="cf_sql_integer" />,
					ValidationRule = <cfqueryparam value="#arguments.Validation.getValidateRule()#" CFSQLType="cf_sql_varchar" />,
					ValidationVal = <cfqueryparam value="#arguments.Validation.getValidationVal()#" CFSQLType="cf_sql_varchar" />,
					ValidationMsg = <cfqueryparam value="#arguments.Validation.getValidationMsg()#" CFSQLType="cf_sql_varchar" null="#not len(arguments.Validation.getValidationMsg())#" />
				WHERE	ValidationId = <cfqueryparam value="#arguments.Validation.getValidationId()#" CFSQLType="cf_sql_integer" />' />
                
        		<cfset result.ValidationId = "#arguments.Validation.getValidationId()#" />
				<cfreturn result />
			</cfcatch>
		</cftry>
        <cfset result.success = true />
        <cfset result.catch = "No errors found" />
        <cfset result.query = '
               UPDATE	aycValidations
				SET
					FieldId = <cfqueryparam value="#arguments.Validation.getFieldId()#" CFSQLType="cf_sql_integer" />,
					ValidationRule = <cfqueryparam value="#arguments.Validation.getValidateRule()#" CFSQLType="cf_sql_varchar" />,
					ValidationVal = <cfqueryparam value="#arguments.Validation.getValidationVal()#" CFSQLType="cf_sql_varchar" />,
					ValidationMsg = <cfqueryparam value="#arguments.Validation.getValidationMsg()#" CFSQLType="cf_sql_varchar" null="#not len(arguments.Validation.getValidationMsg())#" />
				WHERE	ValidationId = <cfqueryparam value="#arguments.Validation.getValidationId()#" CFSQLType="cf_sql_integer" />' />
        
        <cfset result.ValidationId = "#arguments.Validation.getValidationId()#" />      
		<cfreturn result />
	</cffunction>

	<cffunction name="delete" access="public" output="false" returntype="struct">
		<cfargument name="Validation" type="Validation" required="true" />
		
        <cfset var result = structNew()>
		<cfset var qDelete = "">
		<cftry>
			<cfquery name="qDelete" datasource="#variables.dsn#">
				DELETE FROM	aycValidations 
				WHERE	ValidationId = <cfqueryparam value="#arguments.Validation.getValidationId()#" CFSQLType="cf_sql_integer" />
			</cfquery>
			<cfcatch type="database">
				<cfset result.success = false />
                <cfset result.catch = cfcatch />
            	<cfset result.query = '
                DELETE FROM	aycValidations 
				WHERE	ValidationId = <cfqueryparam value="#arguments.Validation.getValidationId()#" CFSQLType="cf_sql_integer" />' />
                
        		<cfset result.ValidationId = "#arguments.Validation.getValidationId()#" />
                <cfreturn result />
			</cfcatch>
		</cftry>
		<cfset result.success = true />
        <cfset result.catch = "No errors found" />
        <cfset result.query = '
                DELETE FROM	aycValidations 
				WHERE	ValidationId = <cfqueryparam value="#arguments.Validation.getValidationId()#" CFSQLType="cf_sql_integer" />' />
        
        <cfset result.ValidationId = "#arguments.Validation.getValidationId()#" />
        <cfreturn result />
	</cffunction>

	<cffunction name="exists" access="public" output="false" returntype="boolean">
		<cfargument name="Validation" type="Validation" required="true" />

		<cfset var qExists = "">
		<cfquery name="qExists" datasource="#variables.dsn#" maxrows="1">
			SELECT count(1) as idexists
			FROM	aycValidations
			WHERE	ValidationId = <cfqueryparam value="#arguments.Validation.getValidationId()#" CFSQLType="cf_sql_integer" />
		</cfquery>

		<cfif qExists.idexists>
			<cfreturn true />
		<cfelse>
			<cfreturn false />
		</cfif>
	</cffunction>

	<cffunction name="save" access="public" output="false" returntype="struct">
		<cfargument name="Validation" type="Validation" required="true" />
		
		<cfset var result = "" />
		<cfif exists(arguments.Validation)>
			<cfset result = update(arguments.Validation) />
		<cfelse>
			<cfset result = create(arguments.Validation) />
		</cfif>
		
		<cfreturn result />
	</cffunction>

	<cffunction name="queryRowToStruct" access="private" output="false" returntype="struct">
		<cfargument name="qry" type="query" required="true">
		
		<cfscript>
			/**
			 * Makes a row of a query into a structure.
			 * 
			 * @param query 	 The query to work with. 
			 * @param row 	 Row number to check. Defaults to row 1. 
			 * @return Returns a structure. 
			 * @author Nathan Dintenfass (nathan@changemedia.com) 
			 * @version 1, December 11, 2001 
			 */
			//by default, do this to the first row of the query
			var row = 1;
			//a var for looping
			var ii = 1;
			//the cols to loop over
			var cols = listToArray(qry.columnList);
			//the struct to return
			var stReturn = structnew();
			//if there is a second argument, use that for the row number
			if(arrayLen(arguments) GT 1)
				row = arguments[2];
			//loop over the cols and build the struct from the query row
			for(ii = 1; ii lte arraylen(cols); ii = ii + 1){
				stReturn[cols[ii]] = qry[cols[ii]][row];
			}		
			//return the struct
			return stReturn;
		</cfscript>
	</cffunction>

</cfcomponent>


